package com.graph;

import com.collection.Bag;
import edu.princeton.cs.algs4.In;

public class Digraph {
    private final int V;
    private int E;
    private Bag<Integer>[] adj;
    private int[] indegree;

    public Digraph(int V) {
        this.V = V;
        this.E = 0;
        adj = (Bag<Integer>[]) new Bag[V];
        for (int i = 0; i < V; i++) {
            adj[i] = new Bag<>();
        }
    }


    public Digraph(In in) {

        V = in.readInt();
        indegree = new int[V];
        adj = (Bag<Integer>[]) new Bag [V];
        int E = in.readInt();
        for (int i = 0; i < V; i++) {
            adj[i] = new Bag<>();
        }
        for (int i = 0; i < E; i++) {
            int v = in.readInt();
            int w = in.readInt();
            addEdge(v, w);
        }


    }

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }




    public void addEdge(int v, int w) {
        adj[v].add(w);
        indegree[w]++;
        E++;
    }

    public Iterable<Integer> adj (int v) {
        return adj[v];
    }

    public Digraph reverse() {
        Digraph r = new Digraph(V);

        for (int i = 0; i < V; i++) {
            for (int w : adj[i]) {
                r.addEdge(w, i);
            }
        }

        return r;
    }





}
